//
//  dEngineOpenGLView.m
//  dEngine
//
//  Created by Daniel Corleone on 23/6/14.
//
//

#import "dEngineOpenGLView.h"
#import <OpenGL/gl.h>

#import "dEngine.h"
#import "filesystem.h"
#import "renderer.h"
#import "commands.h"
#import "camera.h"
#import "timer.h"

#include <sys/types.h>
#include <sys/sysctl.h>

@implementation dEngineOpenGLView
@synthesize animating;
@synthesize animationFrameInterval;

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
        [self initGL: frame];
    }
    return self;
}

- (void) awakeFromNib {
    [self initGL: self.bounds];
    return;
}

- (void)drawRect:(NSRect)dirtyRect
{
    [super drawRect:dirtyRect];
    
    // Drawing code here.
    dEngine_HostFrame();

    
    [[self openGLContext] flushBuffer];     // glFlush();
    
    return;
}

- (void) clearGLContext {
    [super clearGLContext];
    ///
    
    return;
}

- (void) reshape {
    [super reshape];
    
    glViewport(0, 0, self.bounds.size.width, self.bounds.size.height);
    return;
}

- (void) initGL: (CGRect)frame
{
    renderer.statsEnabled = [[NSUserDefaults standardUserDefaults] stringForKey: @"StatisticsEnabled"].boolValue;
//    bool fixedDesired = [[NSUserDefaults standardUserDefaults] stringForKey: @"RendererType"].boolValue;
    
    if ([[NSUserDefaults standardUserDefaults] stringForKey: @"MaterialQuality"] == nil ||
        [[NSUserDefaults standardUserDefaults] stringForKey: @"MaterialQuality"].boolValue) {
        renderer.materialQuality = MATERIAL_QUALITY_HIGH;
    }
    else {
        renderer.materialQuality = MATERIAL_QUALITY_LOW;
    }

    CGRect _screenRect = frame;
    NSInteger vp_width = _screenRect.size.width;
    NSInteger vp_height = _screenRect.size.height;
    
    // init the dEngine renderer
    dEngine_Init(GL_20_RENDERER, (int)vp_width, (int)vp_height);
   
    //Set shadow enabled/disabled
    //Set shadow type
    if ([[NSUserDefaults standardUserDefaults] stringForKey:@"ShadowType"] == nil ||
        [[NSUserDefaults standardUserDefaults] stringForKey:@"ShadowType"].boolValue)
        renderer.props |= PROP_SHADOW ;
    else
        renderer.props &= ~PROP_SHADOW ;
    
    
    if ([[NSUserDefaults standardUserDefaults] stringForKey:@"NormalMappingEnabled"] == nil ||
        [[NSUserDefaults standardUserDefaults] stringForKey:@"NormalMappingEnabled"].boolValue)
        renderer.props |= PROP_BUMP ;
    else
        renderer.props &= ~PROP_BUMP ;
    
    
    if ([[NSUserDefaults standardUserDefaults] stringForKey:@"SpecularMappingEnabled"] == nil ||
        [[NSUserDefaults standardUserDefaults] stringForKey:@"SpecularMappingEnabled"].boolValue)
        renderer.props |= PROP_SPEC ;
    else
        renderer.props &= ~PROP_SPEC ;

    animating = FALSE;
    
    return;
}

- (void) startAnimation
{
    if (animating == NO) {
        timer = [NSTimer timerWithTimeInterval: 1.0/60.0 target: self selector: @selector(tick:) userInfo: nil repeats: YES];
        [[NSRunLoop currentRunLoop] addTimer: timer forMode: NSDefaultRunLoopMode];
        
        animating = YES;
    }
    return;
}

- (void) tick: (NSTimer*)_timer {
    [self drawRect: self.bounds];
    return;
}

@end
